package indi.wshape1.takeawaydeliveryinfo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author Wshape1
 * @version 1.0
 * @description 处理退出登录请求
 * @since 2023-01-18
 */

@Controller
public class QuitController {

    /**
     * 执行退出登录操作
     * 移除session.phone application.{phone} cookie:noLogin
     *
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/quit")
    public String excQuit(HttpServletRequest request, HttpServletResponse response) {
        Object phoneObj = request.getSession().getAttribute("phone");
        if (phoneObj != null) {
            String phone = (String) phoneObj;
            request.getSession().removeAttribute("phone");
            request.getServletContext().removeAttribute(phone);

            for (Cookie cookie : request.getCookies()) {
                if ("noLogin".equals(cookie.getName())) {
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    break;
                }
            }

        }

        return "redirect:/";
    }

}
